package com.lms.micrometer;

import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

import java.sql.SQLOutput;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-06-12 15:51
 * @Description:
 */
public class JvmMetricsDemo {
    public static void main(String[] args) {
//        Logger logger = LoggerFactory.getLogger(JvmMetricsDemo.class);
        JvmGcMetrics jvmGcMetrics = new JvmGcMetrics();
        JvmMemoryMetrics jvmMemoryMetrics = new JvmMemoryMetrics();
        JvmThreadMetrics jvmThreadMetrics = new JvmThreadMetrics();
        ClassLoaderMetrics classLoaderMetrics = new ClassLoaderMetrics();
        SimpleMeterRegistry simpleMeterRegistry = new SimpleMeterRegistry();
        jvmMemoryMetrics.bindTo(simpleMeterRegistry);
        jvmGcMetrics.bindTo(simpleMeterRegistry);
        jvmThreadMetrics.bindTo(simpleMeterRegistry);
        classLoaderMetrics.bindTo(simpleMeterRegistry);
        List<Meter> meters = simpleMeterRegistry.getMeters();
        meters.forEach(meter -> {
            Iterable<Measurement> measure = meter.measure();
            Meter.Id id = meter.getId();
            String name = id.getName();
            List<Tag> tags = id.getTags();
            Meter.Type type = id.getType();
            String baseUnit = id.getBaseUnit();
            System.out.println("name:" + name);
            System.out.println("type:" + type);
            System.out.println("baseUnit:" + baseUnit);
            tags.forEach(tag -> System.out.println("key:" + tag.getKey() + ",value:" + tag.getValue()));
            measure.forEach(measurement -> System.out.println("measure:" + measurement.toString()));
            System.out.println("--------------");

        });
        Metrics.addRegistry(simpleMeterRegistry);

    }
}